package datasource;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.text.PDFTextStripperByArea;

import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class RemoveTextElementFromPDFOld {

    private static final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors();

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);

        try (PDDocument document = PDDocument.load(new File("D:\\yummeng\\test.pdf"))) {
            String textToRemove = "Evaluation Warning : The document was created with Spire.PDF for Java.";

            for (PDPage page : document.getPages()) {
                executor.submit(() -> {
                    try {
                        removeTextFromPage(document, page, textToRemove);
                    } catch (IOException e) {
                        System.err.println("处理页面时出错: " + e.getMessage());
                    }
                });
            }

            executor.shutdown();
            executor.awaitTermination(1, TimeUnit.MINUTES);

            document.save("D:\\yummeng\\output_modified123.pdf");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void removeTextFromPage(PDDocument document, PDPage page, String textToRemove) throws IOException {
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        stripper.setSortByPosition(true);
        Rectangle2D pageRect = new Rectangle2D.Float(0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
        stripper.addRegion("fullpage", pageRect);
        stripper.extractRegions(page);

        Iterator<PDStream> contentStreams = page.getContentStreams();
        List<PDStream> newContentStreams = new ArrayList<>();

        while (contentStreams.hasNext()) {
            PDStream contentStream = contentStreams.next();
            if (contentStream != null) {
                try {
                    byte[] contentBytes = contentStream.toByteArray();
                    String contentString = new String(contentBytes);

                    // 使用 StringBuilder 进行字符串替换
                    StringBuilder builder = new StringBuilder(contentString);
                    int index;
                    while ((index = builder.indexOf(textToRemove)) != -1) {
                        builder.delete(index, index + textToRemove.length());
                    }

                    byte[] newContentBytes = builder.toString().getBytes();

                    PDStream newContentStream = new PDStream(document);
                    try (OutputStream outputStream = newContentStream.createOutputStream()) {
                        outputStream.write(newContentBytes);
                    }
                    newContentStreams.add(newContentStream);
                } catch (IOException e) {
                    System.err.println("处理内容流时出错: " + e.getMessage());
                }
            }
        }

        page.setContents(newContentStreams);
    }
}
